|
|
|
|
|
|
|
Updating a field valueThe SQL UPDATE statement can be used to change the value of multiple fields and multiple records, as in the following example: |
|
|
|
|
|
|
|
|
UPDATE FishSales SET Price=Price*2 WHERE FishCode=1001 |
|
|
|
|
|
|
|
|
The sample UPDATE statement causes the value of the Price field to be doubled for each record where the value of the FishCode field is equal to 1001. |
|
|
|
|
|
|
|
|
Deleting recordsThe DELETE statement allows you to remove records from a table: |
|
|
|
|
|
|
|
|
DELETE From FishSales WHERE FishCode=1001 AND Price > 100 |
|
|
|
|
|
|
|
|
Be careful when using the DELETE statementif you leave out the WHERE clause, every record will be deleted! |
|
|
|
|
|
|
|
|
Adding new recordsThe INSERT statement allows you to specify the field values for a new record. The following example lists the new fields in a Values() clause in the same order as the destination table: |
|
|
|
|
|
|
|
|
INSERT INTO FishSales Values(1002,19.95) |
|
|
|
|
|
|
|
|
Note that if your table contains an AutoNumber field you do not specify a value in the INSERT statement. |
|
|
|
|
|
|
|
|
In order to use an action query, you can just use the SQL statement as a parameter to the Database object's Execute method, as in the following example: |
|
|
|
|
|
|
|
|
dbFish.Execute "INSERT INTO FishSales Values(1003,299.00)" |
|
|
|
|
|
|
|
|
If your query will be executed repeatedly or contains parameters, you may want to create a QueryDef object containing the SQL statement: |
|
|
|
|
|
|
|
|
Dim NewQry As QueryDef
Set NewQry = dbFish.CreateQueryDef("MyQueryDef","DELETE FROM FishSales")
NewQry.Execute
dbFish.DeleteQueryDef("MyQueryDef") |
|
|
|
|
|
|
|
|
Notice that action queries do not return records, so you do not need any recordset objects in your code. Although the types of actions performed by such queries could be achieved by retrieving a recordset from the database and using methods such as AddNew and Delete, an action query is usually more efficient because it is handled by the database engine. |
|
|
|
|
|
|
|
|
Understanding Other Programming Commands |
|
|
|
|
|
|
|
|
So far in this chapter, you have learned how to find specific records and how to move through a group of records. However, in most programs, you also must add, modify, and delete records. The commands covered in the following sections apply only to tables and dynasets (remember that snapshots are not updateable). |
|
|
|
|
|
|
|
|
To add a new record to a recordset, use the AddNew method. AddNew does not actually add the record to the recordset; it clears the copy buffer to allow information for the new record to be |
|
|
|
|
|